package text
{
	import flash.display.JointStyle;
	import flash.display.LineScaleMode;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import mx.core.UIComponent;
	
	import text.textInputEditor;
	
	public class UiTextObject extends UIComponent
	{
		public static const MODE_SCALE:String = "modeScale";
		public static const MODE_MOVE:String = "modeMove";
		public static const MODE_ROTATE:String = "modeRotate";
		public static const MODE_NONE:String = "modeNone";
		
		private var _selected:Boolean;
		private var _textField:TextField;
		private var bg:Shape;
		private var anchor:Array = new Array;
		private var startPos:Point;
		private var currentAnchor:ScaleAnchor;
		private var currentMode:String = MODE_NONE;
		private var _textformat:TextFormat;
		
		public function set selected($value:Boolean):void
		{
			_selected = $value;
			if(_textField)
				makeFilter();
		}
		
		public function get selected():Boolean
		{
			return _selected;
		}
		
		public function get textInfo():Object
		{
			var obj:Object = new Object;
			obj.text = _textField.text;
			obj.fontSize = _textField.getTextFormat().size;
			obj.htmltext = _textField.htmlText;
			return obj;
		}
		
		public function get textformat():TextFormat
		{
			return _textformat;
		}
		
		private function setBrTag():void
		{
			trace("_textField.numLines =",_textField.numLines);
			var cnt:int = _textField.numLines;
			var temp:String = "";
			for(var i:int = 0 ; i < cnt; i++)
			{
				temp += _textField.getLineText(i) + "\n";
			}
			
			_textField.text = temp;
		}
		
		public function get textfiels():TextField
		{
			return _textField;
		}
		
		public function UiTextObject()
		{
			super();
			init();
		}
		
		private function init():void
		{
			bg = new Shape;
			
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			addEventListener(MouseEvent.MOUSE_DOWN, onPress);
			//addEventListener(MouseEvent.MOUSE_UP, onScaleRelease);
		}
		
		public function set text($value:textInputEditor):void
		{
			if(_textField)
				removeChild(_textField);

			_textField = new TextField;
			_textField.antiAliasType = AntiAliasType.ADVANCED;
			x = $value.displayPos.x;
			y = $value.displayPos.y;
			startPos = new Point(x,y);
			_textField.selectable = false;
			_textField.border = true;
			_textField.borderColor = 0xFF6600;
			_textField.multiline = true;
			_textField.width = $value.textObject.textfield.getExplicitOrMeasuredWidth();
			_textField.height = $value.textObject.textfield.getExplicitOrMeasuredHeight();
			
			_textField.embedFonts = true;
			
			_textformat = $value.textObject.textfield.getTextFormat();
			
			
			_textField.wordWrap = true;
			_textField.mouseEnabled = false;
			_textField.mouseWheelEnabled = false;
			
			_textField.text = $value.textObject.text;
			_textField.htmlText = $value.htmltext;
			addChild(_textField);
			makeFilter();

			addChild(bg);
			makeAnchor();
			updateSelection();
			callLater(updateAnchorPosition, [bg.getBounds(this)]);
		}
		
		private function makeAnchor():void
		{
			var scaleObject:ScaleAnchor;
			for(var i:int = 0 ; i < 4 ;i++)
			{
				if(anchor[i])
					removeChild(anchor[i] as ScaleAnchor);
				
				scaleObject = new ScaleAnchor;
				scaleObject.direction = i;
				addChild(scaleObject);
				anchor[i] = scaleObject;
			}
		}
		
		public function updateSelection($rec:Rectangle = null):void
		{
			var rec:Rectangle;
			if($rec)
			{
				rec = $rec;
				rec.x = rec.x;
				rec.y = rec.y;
			}
			else
			{
				rec = _textField.getBounds(this);
			}
			
			bg.graphics.clear();
			bg.graphics.beginFill(0xFFFFFF, 0);
			//bg.graphics.lineStyle(2, 0xFF6600, 1, true, LineScaleMode.NONE, null, JointStyle.BEVEL);
			bg.graphics.drawRect(rec.x,rec.y,rec.width,rec.height);
			bg.graphics.endFill();
			callLater(updateAnchorPosition,[bg.getBounds(this)]);
		}
		
		private function updateScaleList():void
		{
			var rec:Rectangle = bg.getBounds(this);
			
			this.x = startPos.x + rec.x;
			this.y = startPos.y + rec.y;

			updateSelection(new Rectangle(0,0,_textField.textWidth + 5, _textField.textHeight + 4));
			
			makeAnchor();
			
			_textField.x = _textField.y = 0;
			
			_textField.width = _textField.textWidth + 5;
			_textField.height = _textField.textHeight + 5;
			
		}
		
		private function updateAnchorPosition($value:Rectangle):void
		{
			for each(var obj:ScaleAnchor in anchor)
			{
				obj.setPosition($value);
			}
			
		}

		
		/** MouseEvent  **/
		private function onMouseOver(e:MouseEvent):void
		{
			
		}
		
		private function onMouseOut(e:MouseEvent):void
		{
			/*
			if(currentMode == MODE_NONE)
				_textField.filters = null;
			*/
		}
		
		private function makeFilter():void
		{
			if(_selected)
			{
				trace("makeFilter", this, _selected);
				var filter:DropShadowFilter = new DropShadowFilter(0,0,0xFF6600,1,2,2,4);
				var arr:Array = new Array;
				arr.push(filter);
				_textField.filters = arr;
			}
			else
			{
				trace("makeFilter", this, _selected);
				_textField.filters = null;
			}
		}
		
		/** anchor MouseEvent **/
		private function onPress(e:MouseEvent):void
		{
			if(e.target is ScaleAnchor)
			{
				currentMode = MODE_SCALE;
				currentAnchor = e.target as ScaleAnchor;
				currentAnchor.position = new Rectangle(currentAnchor.x, currentAnchor.y, bg.width, bg.height);
				startPos = new Point(this.x, this.y);
				addEventListener(MouseEvent.MOUSE_UP, onScaleRelease);
				stage.addEventListener(MouseEvent.MOUSE_MOVE, onScaleMove);
				stage.addEventListener(MouseEvent.MOUSE_UP, onScaleRelease);
			}
			else
			{
				startDrag();
				currentMode = MODE_MOVE;
				stage.addEventListener(MouseEvent.MOUSE_UP, onRelease);
				addEventListener(MouseEvent.MOUSE_UP, onRelease);
				startPos = new Point(this.x, this.y);
			}
		}
		
		private function onScaleRelease(e:MouseEvent):void
		{
			currentMode = MODE_NONE;
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onScaleMove);
			removeEventListener(MouseEvent.MOUSE_UP, onScaleRelease);
			stage.removeEventListener(MouseEvent.MOUSE_UP, onScaleRelease);
			updateScaleList();
		}
		
		private function onScaleMove(e:MouseEvent):void
		{
			currentAnchor.x = mouseX;
			currentAnchor.y = mouseY;
			updateSelection(currentAnchor.position);
			callLater(updateTextfieldPosition, [currentAnchor.position]);
		}
		
		private function updateTextfieldPosition(rec:Rectangle):void
		{
			//var rec:Rectangle = bg.getBounds(this);
			trace("updateTextfieldPosition =",rec);
			_textField.x = rec.x;
			_textField.y = rec.y;
			_textField.width = rec.width;
			_textField.height = rec.height;
			_textField.scrollH = 0 ;
			_textField.scrollV = 0;
		}
		
		private function onRelease(e:MouseEvent):void
		{
			trace("onRelease");
			currentMode = MODE_NONE;
			stopDrag();
			removeEventListener(MouseEvent.MOUSE_UP, onRelease);
			stage.removeEventListener(MouseEvent.MOUSE_UP, onRelease);
		}
	}
}